/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.restaurant.entity.ReportedLoss;
import org.springblade.modules.restaurant.excel.ReportedLossExcel;
import org.springblade.modules.restaurant.mapper.ReportedLossMapper;
import org.springblade.modules.restaurant.service.IReportedLossService;
import org.springblade.modules.restaurant.vo.ReportedLossVO;
import org.springblade.modules.system.service.IDictService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报损表 服务实现类
 *
 * @author Blade
 * @since 2025-03-31
 */
@Slf4j
@Service
@AllArgsConstructor
public class ReportedLossServiceImpl extends BaseServiceImpl<ReportedLossMapper, ReportedLoss> implements IReportedLossService {

	private IDictService dictService;

	@Override
	public IPage<ReportedLossVO> selectReportedLossPage(IPage<ReportedLossVO> page, ReportedLossVO reportedLoss) {
		return page.setRecords(baseMapper.selectReportedLossPage(page, reportedLoss));
	}

	@Override
	public List<ReportedLossExcel> export(List<ReportedLoss> list) {
		try {
			if (list.isEmpty()) {
				log.error("报损记录为空");
				return new ArrayList<>();
			}
			return list.stream().map(item -> {
				ReportedLossExcel reportedLossExcel = new ReportedLossExcel();
				reportedLossExcel.setDeclareDate(item.getDeclareDate());
				reportedLossExcel.setUserName(item.getUserName());
				reportedLossExcel.setAreaName(item.getAreaName());
				reportedLossExcel.setItemName(item.getItemName());
				reportedLossExcel.setItemType(dictService.getValue("item_type", item.getItemType()));
				reportedLossExcel.setEnclosure(item.getEnclosure());
				reportedLossExcel.setRemark(item.getRemark());
				return reportedLossExcel;
			}).toList();
		} catch (Exception e) {
			log.error("导出报损记录失败", e);
			return List.of();
		}
	}

}
